﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;

namespace UI
{
    public class UIFieldRunMapTopInfo : MonoBehaviour
    {
        public UILabel netSpeed;
        public UILabel coin;
        public UILabel diamond;

        public UILabel kills;
        public UILabel beKills;

        public void Init()
        {
            netSpeed.text = "";
            
            SetCoin(StaticData.playerData.coin);
            SetDiamond(StaticData.playerData.diamond);
            kills.text = StaticData.fieldData.kills.ToString();
            beKills.text = StaticData.fieldData.beKills.ToString();
        }

        public void Refresh()
        {
            Init();
        }
        
        void SetCoin(long coin)
        {
            //if(coin > 10000)
            //{
            //    var intCoin = (int)(coin * 0.0001f);
            //    this.coin.text = intCoin + TableTextBlo.GetText("wan");
                
            //}
            //else
            //{
            //    this.coin.text = coin+"";
            //}

            this.coin.text = CommonMethod.GetShowValue(coin);
        }

        void SetDiamond(long diamond)
        {
            //if (diamond > 100000)
            //{
            //    var intCoin = (int)(diamond * 0.0001f);
            //    this.diamond.text = intCoin + TableTextBlo.GetText("wan");
            //}
            //else
            {
                this.diamond.text = diamond + "";
            }
        }

        public void SetSpeed(SCPingTcp ping)
        {
            returnPkg = true;
            usedTime = Time.time - sendTime;
            var showTime = (int)(usedTime * 1000f);
            netSpeed.text = "Ping:"+showTime + "ms";
            
        }


        public float timer = 0f;
        public float sendTime;
        public float usedTime;
        public bool returnPkg;

        void Update()
        {
            timer -= Time.deltaTime;
            if (timer <= 0f)
            {
                SendPing();
                timer = 3f;
            }
        }

        void SendPing()
        {
            if (returnPkg)
            {
                returnPkg = false;
                sendTime = Time.time;
                var ping = new CSPingTcp();
                ping.clientTime = Battle.Engine.CommonUtil.GetTimeNow();

                NetAPI.SendSocket(OpDefine.CSPingTcp, ping);
            }
        }
    }
}